﻿using CMS.Models;
using CMS.View;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class MemberViewModel:ViewModelBase
    {
        public AppData AppData { get; set; } = AppData.Instance;
        private List<Menber> menbers = new List<Menber>();
        public List<Menber> Menbers
        {
            get
            {
                return menbers;
            }
            set
            {
                menbers = value;
                RaisePropertyChanged(nameof(Menbers));
            }
        }
        //初始化刷新数据
        public MemberViewModel()
        {
            Menbers = new MemberProvider().Select();
        }
        //删除数据
        public RelayCommand<object> DeleteMenberCommand
        {
            get
            {
                return new RelayCommand<object>((arg) =>
                {
                    if (!(arg is Menber model)) return;
                    var count = new MemberProvider().Delete(model);
                    if (count > 0) MessageBox.Show("操作成功", "温馨提示");

                    Menbers = new MemberProvider().Select();//刷新数据
                });
            }
        }
        //编辑数据
        public RelayCommand<object> EditMenberCommand
        {
            get
            {
                return new RelayCommand<object>((arg) =>
                {
                    if (!(arg is Menber model)) return;
                    var count = new MemberProvider().Update(model);
                    if (count > 0) MessageBox.Show("操作成功", "温馨提示");
                    Menbers = new MemberProvider().Select();//刷新数据
                });
            }
        }

        /// <summary>
        /// 打开添加新纪录对话框
        /// </summary>
        /// 
        public RelayCommand OpenMemberWindowCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    var window = new AddMenberWindow();
                    window.ShowDialog();
                    //刷新数据 注意大写不要小写
                    Menbers = new MemberProvider().Select();//刷新数据
                });
            }
        }
    }
}
